

import org.w3c.dom.*;
import java.io.*;
import javax.xml.parsers.*;
import java.util.*;
import java.util.zip.*;

import datolib.*;

/**
* Class to represent a Java application to run from DatoliteApplication
*
* @file DatoliteApplication.java
* @author Blair Harris
*/
public class DatoliteApplication {

	private String applicationID;
	private String applicationClassString;
	private String designDirectory;
	private String uipFile;
	
	private Class<?> applicationClass;
	private Object applicationObject;
	
	PlatformImplementation defalt;
	PlatformImplementation mobile;
	PlatformImplementation tablet;
	PlatformImplementation desktop;
	
//	private PlatformList platformList;
	
	/**
	* Constructor that assigns the important private member variables
	*
	* @param id
	*   The id or name of the applicationClass
	* @param application
	*   The name of the Java class that contains the application functionality
	* @param instanceClass
	*   The name of the Java class that contains the instance functionality
	* @param dir
	*   The directory that the application design (GUI) files can be found in 
	*/
	public DatoliteApplication(String id, String application, String dir) {
	
		this.applicationID = id;
		this.applicationClassString = application;
		this.designDirectory = dir;
		this.uipFile = "";		
		
		this.applicationClass = null;
		this.applicationObject = null;
		
		this.defalt = null;
		this.mobile = null;
		this.tablet = null;
		this.desktop = null;
		
		
		//this.platformList = pl;
	}
	
	/**
	* Get the application ID
	*
	* @return 
	*   The ID of the application
	*/
	public String getApplicationID() {
		return this.applicationID;
	}

	public void install(Platform platform, String gui, String code) {
		
		switch (platform) {
		
			case DEFAULT:
				this.defalt = new PlatformImplementation(this.applicationID, platform, code, gui);
				break;
			case MOBILE:
				this.mobile = new PlatformImplementation(this.applicationID, platform, code, gui);
				break;
			case TABLET:
				this.tablet = new PlatformImplementation(this.applicationID, platform, code, gui);
				break;
			case DESKTOP:				
				this.desktop = new PlatformImplementation(this.applicationID, platform, code, gui);
				break;
			default:
				return;
		}
		
	}

	/**
	* Get the design directory of the application
	*
	* @return
	*   The directory path with the design files
	*/
	public String getDesignDirectory() {
		return this.designDirectory;
	}

	/**
	* Get the .uip file path
	*
	* @return
	*   The path to the .uip file
	*/
	public String getUipFile() {
		return this.uipFile;
	}

	/**
	* Set the .uip file path
	*
	* @param uip
	*   The path to the .uip file
	*/
	public void setUipFile(String uip) {
		this.uipFile = uip;
	}
	
	
	/**
	* Load both the application and instance class and make a new object
	* of the application class
	*
	* @return
	*   True of the classes and object was loaded otherwise false 
	*/
	public boolean createApplicationObject() {

		
		try {
			DatoliteClassLoader loader = new DatoliteClassLoader();
        	this.applicationClass = loader.loadClass(this.applicationClassString);
        	
        	this.applicationObject = this.applicationClass.newInstance();
        	
        	return true;
        } catch (Exception e) {
        	e.printStackTrace();
        	return false;
        }
	}
	
	/**
	* Call the checkCredentials method in the application object to validate
	* the user
	*
	* @param username
	*   The user to check
	* @param passwd
	*   The password of the user to check
	* @return
	*   True if the username and password is valid otherwise false
	*/	
	public boolean checkCredentials(String username, String passwd) {
		
		try {
		
			Boolean returnValue = (Boolean)this.applicationClass.getMethod
				("checkCredentials",String.class, String.class).invoke(this.applicationObject, username, passwd);
			
			return returnValue;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	
	
	public DatoliteInstance makeInstance(Platform platform) {
	
		switch (platform) {
		
			case DEFAULT:
				return new DatoliteInstance(this.applicationID, this.defalt.duplicate());
			case MOBILE:
				if (this.mobile == null && this.defalt != null) {
					return this.makeInstance(Platform.DEFAULT);
				} else if (this.mobile != null) {
					return new DatoliteInstance(this.applicationID, this.mobile.duplicate());
				} else {
					return null;
				}
			case TABLET:
				if (this.tablet == null && this.defalt != null) {
					return this.makeInstance(Platform.DEFAULT);
				} else if (this.tablet != null) {
					return new DatoliteInstance(this.applicationID, this.tablet.duplicate());
				} else {
					return null;
				}
			case DESKTOP:
				if (this.desktop == null && this.defalt != null) {
					return this.makeInstance(Platform.DEFAULT);
				} else if (this.desktop != null) {
					return new DatoliteInstance(this.applicationID, this.desktop.duplicate());
				} else {
					return null;
				}
			default:
				return null;
		}
	}
	
}
